package ch11.fileChooser;

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Image;
import java.io.File;

import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.filechooser.FileView;

public class FileChooserTest {
	public static void main(String[] args) {
		EventQueue.invokeLater(() -> {
			var frame = new ImageViewerFrame();
			frame.setTitle("FileChooserTest");
			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			frame.setVisible(true);
		});
	}
}

class ImageViewerFrame extends JFrame {
    private static final int DEFAULT_WIDTH = 300;
    private static final int DEFAULT_HEIGHT = 400;
    private JLabel label;
    private JFileChooser chooser;

    public ImageViewerFrame() {
        setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

        var menuBar = new JMenuBar();
        setJMenuBar(menuBar);

        var menu = new JMenu("File");
        menuBar.add(menu);

        var openItem = new JMenuItem("Open");
        menu.add(openItem);
        openItem.addActionListener(_ -> {
            int result = chooser.showOpenDialog(ImageViewerFrame.this);
            if (result != JFileChooser.APPROVE_OPTION) {
                return;
            }

            String name = chooser.getSelectedFile().getPath();
            label.setIcon(new ImageIcon(name));

            // 调整窗口大小
            pack();
        });

        var exitItem = new JMenuItem("Exit");
        menu.add(exitItem);
        exitItem.addActionListener(_ -> System.exit(0));

        label = new JLabel();
        add(label);

        // 文件选择器
        chooser = new JFileChooser();
        chooser.setCurrentDirectory(new File("."));

        // 文件扩展名过滤器
        var filter = new FileNameExtensionFilter(
                "Image Files", "jpg", "gif", "png"
        );
        chooser.setFileFilter(filter);

        // 附件, 预览图片
        chooser.setAccessory(new ImagePreviewer(chooser));

        // 文件图标过滤器
        chooser.setFileView(new FileIconView(filter, getIcon("palette.gif")));
    }

    private Icon getIcon(String name) {
        var url = ImageViewerFrame.class.getResource(name);
        return url == null ? null : new ImageIcon(url);
    }
}

class FileIconView extends FileView {
    private FileFilter filter;
    private Icon icon;

    public FileIconView(FileFilter filter, Icon icon) {
        this.filter = filter;
        this.icon = icon;
    }

    @Override
    public Icon getIcon(File f) {
        if (!f.isDirectory() && filter.accept(f)) {
            return icon;
        }
        return null;
    }
}

// 附件 chooser.setAccessory
class ImagePreviewer extends JLabel {
    public ImagePreviewer(JFileChooser chooser) {
        setPreferredSize(new Dimension(100, 100));
        setBorder(BorderFactory.createEtchedBorder());

        // JavaBeans 事件模型
        chooser.addPropertyChangeListener(event -> {
            if (event.getPropertyName() != JFileChooser.SELECTED_FILE_CHANGED_PROPERTY) {
                return;
            }

            File f = (File) event.getNewValue();
            if (f == null) {
                setIcon(null);
                return;
            }

            var icon = new ImageIcon(f.getPath());

            if (icon.getIconWidth() > getWidth()) {
                icon = new ImageIcon(icon.getImage().getScaledInstance(getWidth(), -1, Image.SCALE_DEFAULT));
            }

            setIcon(icon);
        });
    }
}
